A r t i c l e s
Navigation

Note: This site is
a bit older, personal views
may have changed.

M a i n P a g e

D i r e c t o r y

MD5Hash Example


Download working example and binaries: http://z505.com/pascal/MD5Hash/MD5Hash.zip

Note: the old function that Alex was questioning about (which was causing issues) is suffixed _Alex, my examples are suffixed with _L505.


 
program Project1;

{$mode objfpc}{$H+}


{ Summary:
    Get MD5 hash for a string. Test program.

  Authors:
    Lars (L505)
}

uses
  sysutils, md5;

function MakeMD5Hash_L505(aValue: pchar; MD5_Rslt: pchar): longint;  stdcall; external 'mydll.dll';
function GetMD5HashSize_L505(aValue: pchar): longint;  stdcall; external 'mydll.dll';

var
  tmp: pchar;
  InputText: pchar;
  MD5HashText: string;
  MD5HashSize: longint;
begin

  InputText:= 'Please MD5 me.';

  // get size of hash first
  MD5HashSize:= GetMD5HashSize_L505(InputText);
  
  // allocate buffer on the CALLING SIDE !!
  tmp:= StrAlloc(MD5HashSize);

  // now get the hash result into temporary buffer 'tmp'
  MakeMD5Hash_L505(InputText, tmp);

  write('The hash value output is: ');
  writeln(tmp);
  
  // free buffer on the CALLING SIDE !!
  StrDispose(tmp);

  writeln;
  writeln('Bye Bye');
  writeln('Press ENTER to exit');
  readln;
  
{ NOTE:

  Since you know the hash size (32 + 1), there is no need to call GetMD5HashSize
  I just included this as an example if you DID NOT know the size of the hash
  result.
}
end.

library mydll; {$mode objfpc}{$H+} uses md5, sysutils; { Summary: Create a MD5 hash from a string and return the result. Authors: Alexandre Leclerc Lars (L505) } function MakeMD5Hash_Alex(aValue: pchar): pchar; stdcall; var s: string; size: Integer; begin s := MD5Print(MD5String(aValue)); size := Length(s) + 1; Result := nil; GetMem(Result,size); // Result := StrAlloc(size); //Use strings; Move(Pointer(s)^, Result^, size); end; { return the hash result based on input aValue } function MakeMD5Hash_L505(aValue: pchar; MD5_Rslt: pchar): longint; stdcall; var mystr: string; // local variable, dangerous to point to unless copied temp: pchar; // temporary pchar size: Integer; begin mystr:= MD5Print(MD5String(aValue)); size:= Length(mystr) + 1; // return the size of the result Result:= size; temp:= pchar(mystr); // careful, temp is pointing to a local variable mystr! {out} {in} StrCopy(MD5_Rslt, temp); // temp is now copied to 'rslt' parameter, and must be, otherwise the calling // program can give access violations end; { Warning: mystr is freed at the end of this function. BAD CODE: MD5_Rslt:= pchar(mystr); // MD5_rslt is pointing to a local variable, mystr, // already freed when we try to use it in the EXE! } { return the size of the hash specifically } function GetMD5HashSize_L505(aValue: pchar): longint; stdcall; var mystr: string; size: Integer; begin mystr:= MD5Print(MD5String(aValue)); size:= Length(mystr) + 1; // return the size of the result Result:= size; end; exports MakeMD5Hash_Alex, MakeMD5Hash_L505, GetMD5HashSize_L505; end.

See also: Passing PChars in Libraries

About
This site is about programming and other things.
_ _ _